<?php
namespace app\admin\controller;

use app\core\Incontroller;
use think\Request;
use Qiniu\Auth;

class Link extends Incontroller
{

    public function index(){
        if(isset($_POST['draw']) && $_POST['draw']){
            $page_start = intval($_POST['start']);
            $length = intval($_POST['length']);
            $shop = model('link');

            if(isset($_POST['search']['value'])&&$_POST['search']['value']){
                $count = $shop->whereLike('name','%'.$_POST['search']['value'].'%')->count();
                $res   = $shop->whereLike('name','%'.$_POST['search']['value'].'%')->limit($page_start,$length)->order('id','desc')->select();
            }else{
                $count = $shop->count("*");
                $res = $shop->limit($page_start,$length)->order('id','desc')->select();
            }

            $data = [];
            foreach ($res as $val){
                $data[] = array(
                    'id'          => $val->id,
                    'name'        => $val->name,
                    'thumb'     => $val->thumb,
                    'url'         => $val->url,
                    'row_status' => $val->row_status,
                    'sid'         => $val->id
                );
            }
            echo json_encode([
                'draw'            => $_POST['draw'],
                'recordsTotal'    => $count,
                'recordsFiltered' => $count,
                'data'            => $data
            ]);
            exit;
        }

        echo $this->fetch();
    }

    public function add(){
        $qiniu = config('qiniu');
        //添加表单提交
        if(isset($_POST['type']) && $_POST['type']=='save'){
            $data = $this->_getData($qiniu);

            $shop = model('link');
            $res = $shop->insert($data);
            if($res){
                error(0,'添加成功');
                exit;
            }
            error(1,'添加失败');
            exit;
        }
        vendor('qiniu/php-sdk/autoload.php');
        $auth = new Auth($qiniu['accessKey'],$qiniu['secretKey']);
        $token = $auth->uploadToken($qiniu['bucket']); //七牛云上传凭证
        $this->assign('qiniu_token',$token);
        return $this->fetch();
    }

    public function edit(){
        $shop = model('link');
        $qiniu = config('qiniu');
        //添加表单提交
        if(isset($_POST['type']) && $_POST['type']=='save'){
            $data = $this->_getData($qiniu);
            unset($data['create_time']);
            $res = $shop->update($data,['id'=>intval($_POST['id'])]);
            if($res){
                error(0,'修改成功');
                exit;
            }
            error(1,'修改失败');
            exit;
        }
        vendor('qiniu/php-sdk/autoload.php');
        $auth = new Auth($qiniu['accessKey'],$qiniu['secretKey']);
        $token = $auth->uploadToken($qiniu['bucket']); //七牛云上传凭证
        $this->assign('qiniu_token',$token);

        $shop = $shop->where('id',intval($_GET['id']))->find();
        $this->assign('shop',$shop);
        echo $this->fetch();
        exit;
    }

    public function del(){  //删除
        if(!isset($_POST['id']) || !$_POST['id']){
            error(1,'删除失败');
            exit;
        }
        $shop = model('menu');
        $del = $shop->where(['id'=>intval($_POST['id'])])->delete();
        if($del){
            error(0,'删除成功');
            exit;
        }
        error(1,'删除失败');

    }

    protected function _getData($qiniu){
        $data = array(
            'name'       => $_POST['name'],
            'thumb'      => strstr($_POST['thumb'],'http://')? $_POST['thumb']:$qiniu['domain'].$_POST['thumb'],
            'row_status' => $_POST['row_status'],
            'create_time'=> date("Y-m-d H:i:s"),
            'update_time'=> date("Y-m-d H:i:s"),
            'url'        => $_POST['url']
        );
        return $data;
    }
}